﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TlvLib.SiChuan
{
    class TvPacketFactory
    {
        public static TvPacket CreateRegistrationPacket()
        {
            if (string.IsNullOrEmpty(Configuration.AccessCode))
            {
                throw new Exception("系统接入码为空");
            }


            var buffer = Helper.GetBytes(Configuration.AccessCode, 32);
            return new TvPacket() { Type = PacketType.Registration, Value = buffer };
        }

        public static TvPacket CreateHeartBeatPacket()
        {
            var buffer = Configuration.EndianBitConverter.GetBytes(ProtocolConstant.HeartBeat);
            return new TvPacket() { Type = PacketType.HeartBeat, Value = buffer };
        }

        public static TvPacket CreateTimeSyncPacket()
        {
            var buffer = Configuration.EndianBitConverter.GetBytes((short)1);
            return new TvPacket() { Type = PacketType.TimeSync, Value = buffer };
        }

        public static TvPacket CreateDataReportPacket(CarInfo carInfo)
        {
            var destStream = new MemoryStream();

            var jrbh = Helper.GetBytes(Configuration.AccessCode, 32);
            Helper.WriteAllBytes(destStream, jrbh);
            

            var sn = Configuration.EndianBitConverter.GetBytes(carInfo.SN);
            Helper.WriteAllBytes(destStream, sn);
           

            var kkbh = Helper.GetBytes(carInfo.KKBH, 18);
            Helper.WriteAllBytes(destStream, kkbh);
           

            var cdbh = Helper.GetBytes(carInfo.CDBH, 2);
            Helper.WriteAllBytes(destStream, cdbh);
            

            var fxbh = Helper.GetBytes(carInfo.FXBH, 10);
            Helper.WriteAllBytes(destStream, fxbh);
            

            var sbbh = Helper.GetBytes(carInfo.SBBH, 18);
            Helper.WriteAllBytes(destStream, sbbh);
           

            var cthphm = Helper.GetBytes(carInfo.CTHPHM, 18);
            Helper.WriteAllBytes(destStream, cthphm);


            var dummyBuffer = new byte[2 + 2 + 18 + 2 + 2];
            Helper.WriteAllBytes(destStream, dummyBuffer);

            


            var hpyz = Helper.GetBytes("3", 2);
            Helper.WriteAllBytes(destStream, hpyz);
            
            dummyBuffer = new byte[4 + 10 + 10 + 2 + 4 + 4 + 8 + 8];
            Helper.WriteAllBytes(destStream, dummyBuffer);

            var jgsjStr = carInfo.JGSJ.ToString(ProtocolConstant.TimeFormat);
            var jgsj = Helper.GetBytes(jgsjStr, 24);
            Helper.WriteAllBytes(destStream, jgsj);
           
            dummyBuffer = new byte[2 * 14];
            Helper.WriteAllBytes(destStream, dummyBuffer);

            //txsl
            var txsl = Config.EndianBitConverter.GetBytes((short) carInfo.TXLJs.Length);
            Helper.WriteAllBytes(destStream, txsl);

            const int pathLen = 300;
            for (int i = 0; i < 5; i++)
            {
                if (i< carInfo.TXLJs.Length && !string.IsNullOrEmpty(carInfo.TXLJs[i]))
                {
                    var txlj = Helper.GetBytes(carInfo.TXLJs[i], pathLen);
                    Helper.WriteAllBytes(destStream, txlj);
                }
                else
                {
                    dummyBuffer = new byte[pathLen];
                    Helper.WriteAllBytes(destStream, dummyBuffer);
                }
            }


            var resBuf = destStream.ToArray();
            destStream.Dispose();

            return new TvPacket() { Type = PacketType.DataReport, Value = resBuf };
        }

        private void  WriteAllBytes(Stream stream, byte[] buffer)
        {
            stream.Write(buffer, 0, buffer.Length);
        }

        public static TvPacket CreateUnregisterPacket()
        {
            var value = Configuration.EndianBitConverter.GetBytes((short)1);
            return new TvPacket() { Type = PacketType.UnRegist, Value = value };
        }
    }
}
